package com.bw.base.mvvm

import androidx.lifecycle.ViewModel
import com.bw.base.net.RetrofitUtils
import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody

open class BaseViewModel:ViewModel() {
    //将Api实例化
    val apiService : ApiService = RetrofitUtils.getRetrofitUtil().create(ApiService::class.java)

    /**
     * 封装公共的方法，将map格式的参数转换成requestBody的格式
     */
    fun mapToBody(map:MutableMap<String,Any>):RequestBody{
        val json = Gson().toJson(map)
        val body = RequestBody.create("application/json;charset=utf-8".toMediaType(),json)
        return body
    }
}